/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyh.actionitembadge_ohos;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DragInfo;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import com.mikepenz.actionitembadge.library.utils.ToastsUtils;

import hollowsoft.slidingdrawer.OnDrawerCloseListener;
import hollowsoft.slidingdrawer.OnDrawerOpenListener;
import hollowsoft.slidingdrawer.OnDrawerScrollListener;


/**
 * @since 2021-06-19
 */
public class SlidingDrawerTest extends DirectionalLayout implements Component.DrawTask {
    boolean finishedInflating = false;
    float top = 1900.0F;
    float start = 0.0F;
    float _Delta = 0.0F;
    float begening = 0.0F;
    float ending = 0.0F;
    int scrollState = 0;
    private boolean animating = false;
    private boolean locked = false;
    private boolean expanded = false;
    private Component viewHandle;
    private Component viewContent;
    private OnDrawerOpenListener onDrawerOpenListener;
    private OnDrawerCloseListener onDrawerCloseListener;
    private OnDrawerScrollListener onDrawerScrollListener;
    DraggedListener mDragListener;

    /**
     * 抽屉构造器
     * @param var1 上下文
     **/
    public SlidingDrawerTest(Context var1) {
        super(var1);
        this.setOrientation(0);
        this.setClickable(true);
        this.addDrawTask(this);
    }

    /**
     * 抽屉构造器
     * @param var1 上下文
     * @param var2 资源set
     **/
    public SlidingDrawerTest(Context var1, AttrSet var2) {
        super(var1, var2);

        class NamelessClass_1 implements DraggedListener {
            NamelessClass_1() {
            }

            /**
             * 拖拽按压
             * @param var1 组件
             * @param var2 拖拽信息
             **/
            public void onDragDown(Component var1, DragInfo var2) {
            }

            /**
             * 拖拽开始
             * @param var1 组件
             * @param var2 拖拽信息
             **/
            public void onDragStart(Component var1, DragInfo var2) {
                SlidingDrawerTest.this.start = SlidingDrawerTest.this.getOrientation() == 1 ?
                    var2.startPoint.position[1] : var2.startPoint.position[0];
                SlidingDrawerTest.this.updateBounds();
            }

            /**
             * 拖拽更新
             * @param var1 组件
             * @param var2 拖拽信息
             **/
            public void onDragUpdate(Component var1, DragInfo var2) {
                if (!SlidingDrawerTest.this.locked) {
                    if (SlidingDrawerTest.this.scrollState == 0) {
                        if (SlidingDrawerTest.this.onDrawerScrollListener != null) {
                            SlidingDrawerTest.this.onDrawerScrollListener.onScrollStarted();
                        }

                        SlidingDrawerTest.this.scrollState = 1;
                    }

                    float var3 = SlidingDrawerTest.this.start - (SlidingDrawerTest.this.getOrientation() == 1 ?
                        var2.updatePoint.position[1] : var2.updatePoint.position[0]);
                    if (SlidingDrawerTest.this.getOrientation() != 1) {
                        if (var3 > 0.0F && SlidingDrawerTest.this.getCurrentPosition()
                            <= (float) (-SlidingDrawerTest.this.viewContent.getWidth())) {
                            var3 = 0.0F;
                        }

                        if (var3 < 0.0F && SlidingDrawerTest.this.getCurrentPosition() >= 0.0F) {
                            var3 = 0.0F;
                        }

                        if (SlidingDrawerTest.this.getCurrentPosition() > (float) (-SlidingDrawerTest.this.viewContent.getWidth())
                            && SlidingDrawerTest.this.getCurrentPosition() - var3 < (float) (-SlidingDrawerTest.this.viewContent.getWidth())) {
                            var3 = (float) SlidingDrawerTest.this.viewContent.getWidth() + SlidingDrawerTest.this.getCurrentPosition();
                        }

                        if (SlidingDrawerTest.this.getCurrentPosition() < 0.0F && SlidingDrawerTest.this.getCurrentPosition() - var3 > 0.0F) {
                            var3 = SlidingDrawerTest.this.getCurrentPosition();
                        }

                        SlidingDrawerTest.this.createAnimatorProperty().moveByX(-var3).setDuration(0L).start();
                    } else {
                        if (SlidingDrawerTest.this.getCurrentPosition() <= SlidingDrawerTest.this.ending && var3 > 0.0F) {
                            var3 = 0.0F;
                        }

                        if (SlidingDrawerTest.this.getCurrentPosition() >= SlidingDrawerTest.this.begening && var3 < 0.0F) {
                            var3 = 0.0F;
                        }

                        if (SlidingDrawerTest.this.getCurrentPosition() > SlidingDrawerTest.this.ending
                            && SlidingDrawerTest.this.getCurrentPosition() - var3 < SlidingDrawerTest.this.ending) {
                            var3 = -(SlidingDrawerTest.this.ending - SlidingDrawerTest.this.getCurrentPosition());
                        } else if (SlidingDrawerTest.this.getCurrentPosition() < SlidingDrawerTest.this.begening
                            && SlidingDrawerTest.this.getCurrentPosition() + var3 > SlidingDrawerTest.this.begening) {
                            var3 = SlidingDrawerTest.this.begening - SlidingDrawerTest.this.getCurrentPosition();
                        }

                        SlidingDrawerTest.this.createAnimatorProperty().moveByY(-var3).setDuration(0L).start();
                    }

                    SlidingDrawerTest.this._Delta = var3;
                }

            }

            /**
             * 拖拽结束
             * @param var1 组件
             * @param var2 拖拽信息
             **/
            public void onDragEnd(Component var1, DragInfo var2) {
                if (SlidingDrawerTest.this.getOrientation() == 1) {
                    if (SlidingDrawerTest.this._Delta < 0.0F) {
                        SlidingDrawerTest.this.animate(SlidingDrawerTest.this.begening
                            - SlidingDrawerTest.this.getCurrentPosition(), 0);
                    } else {
                        SlidingDrawerTest.this.animate(SlidingDrawerTest.this.ending
                            - SlidingDrawerTest.this.getCurrentPosition(), 0);
                    }
                } else {
                    if (SlidingDrawerTest.this._Delta > 0.0F) {
                        SlidingDrawerTest.this.animate((float) (-SlidingDrawerTest.this.viewContent.getWidth()), 0);
                    } else if (SlidingDrawerTest.this._Delta < 0.0F) {
                        SlidingDrawerTest.this.animate(0.0F, 0);
                    } else if (SlidingDrawerTest.this._Delta == 0.0F) {
                        SlidingDrawerTest.this.animate(0.0F, 0);
                        SlidingDrawerTest.this.onDrawerOpenListener.onDrawerOpened();
                    }
                }
                if (SlidingDrawerTest.this.onDrawerScrollListener != null) {
                    SlidingDrawerTest.this.scrollState = 0;
                    SlidingDrawerTest.this.onDrawerScrollListener.onScrollEnded();
                }
            }

            /**
             * 拖拽取消
             * @param var1 组件
             * @param var2 拖拽信息
             **/
            public void onDragCancel(Component var1, DragInfo var2) {
            }
        }

        this.mDragListener = new NamelessClass_1();
        this.setClickable(true);
        this.addDrawTask(this);
    }

    /**
     * 设置handle
     *
     * @param var1 组件
     **/
    public void setHandle(Component var1) {
        this.addComponent(var1, 0);
    }

    /**
     * 设置holder
     *
     * @param var1 组件
     **/
    public void setHolder(Component var1) {
        this.addComponent(var1);
    }

    protected void onFinishInflate() {
        this.viewHandle = this.getComponentAt(0);
        this.viewContent = this.getComponentAt(1);
        if (this.viewHandle != null && this.viewContent != null) {
            Point var1 = new Point();
            ((Display) DisplayManager.getInstance().getDefaultDisplay(this.getContext()).get()).getSize(var1);
            LayoutConfig var2;
            if (this.getOrientation() == 1) {
                var2 = new LayoutConfig(-1, -2);
                this.top = (float) (((Component) this.getComponentParent()).getHeight() - this.viewHandle.getHeight());
                this.setContentPositionY(this.top);
            } else {
                var2 = new LayoutConfig(-2, -1);
                Component var3 = this.getComponentAt(0);
                this.removeComponentAt(0);
                this.addComponent(var3);
                this.setContentPositionX((float) (-this.viewContent.getWidth()));
            }

            this.setLayoutConfig(var2);
            this.viewHandle.setDraggedListener(1, this.mDragListener);
            this.viewContent.setDraggedListener(1, this.mDragListener);
            this.finishedInflating = true;
        }

    }

    /**
     * 设置onDraw方法
     *
     * @param var1 组件
     * @param var2 画布
     **/
    public void onDraw(Component var1, Canvas var2) {
        if (!this.finishedInflating) {
            this.onFinishInflate();
        }

    }

    /**
     * 设置OnDrawerOpenListener
     *
     * @param var1 抽屉开启监听器
     **/
    public void setOnDrawerOpenListener(OnDrawerOpenListener var1) {
        this.onDrawerOpenListener = var1;
    }

    /**
     * 设置OnDrawerCloseOpenListener
     *
     * @param var1 抽屉关闭监听器
     **/
    public void setOnDrawerCloseListener(OnDrawerCloseListener var1) {
        this.onDrawerCloseListener = var1;
    }

    /**
     * 设置OnDrawerScrollListener
     *
     * @param var1 抽屉滚动监听器
     **/
    public void setOnDrawerScrollListener(OnDrawerScrollListener var1) {
        this.onDrawerScrollListener = var1;
    }

    /**
     * 设置动画
     *
     * @param var1 位置1
     * @param var2 位置2
     **/
    public void animate(final float var1, int var2) {
        if (!this.animating) {
            AnimatorProperty var3 = this.createAnimatorProperty();
            if (this.getOrientation() == 1) {
                var3.moveByY(var1);
            } else {
                var3.moveToX(var1);
            }

            var3.setDuration((long) var2).setStateChangedListener(new Animator.StateChangedListener() {
                public void onStart(Animator var1x) {
                    SlidingDrawerTest.this.animating = true;
                }

                public void onStop(Animator var1x) {
                }

                public void onCancel(Animator var1x) {
                }

                public void onEnd(Animator var1x) {
                    SlidingDrawerTest.this.animating = false;
                    if (SlidingDrawerTest.this.getOrientation() == 1) {
                        if (var1 > 0.0F) {
                            if (SlidingDrawerTest.this.onDrawerCloseListener != null) {
                                SlidingDrawerTest.this.onDrawerCloseListener.onDrawerClosed();
                            }

                            SlidingDrawerTest.this.expanded = false;
                        } else {
                            if (SlidingDrawerTest.this.onDrawerOpenListener != null) {
                                SlidingDrawerTest.this.onDrawerOpenListener.onDrawerOpened();
                            }

                            SlidingDrawerTest.this.expanded = true;
                        }
                    } else {
                        if (var1 > 0.0F || var1 == 0.0F) {
                            if (SlidingDrawerTest.this.onDrawerOpenListener != null) {
                                SlidingDrawerTest.this.onDrawerOpenListener.onDrawerOpened();
                            }

                            SlidingDrawerTest.this.expanded = true;
                        } else {
                            if (SlidingDrawerTest.this.onDrawerCloseListener != null) {
                                SlidingDrawerTest.this.onDrawerCloseListener.onDrawerClosed();
                            }

                            SlidingDrawerTest.this.expanded = false;
                        }
                    }
                }

                public void onPause(Animator var1x) {
                }

                public void onResume(Animator var1x) {
                }
            }).start();
        }

    }

    /**
     * 判断是否在移动
     *
     * @return boolean
     **/
    public final boolean isMoving() {
        return this.scrollState == 1 || this.animating;
    }

    /**
     * 判断是否打开
     *
     * @return boolean
     **/
    public final boolean isOpened() {
        return this.expanded;
    }

    /**
     * 滚动锁
     **/
    public final void scrollLock() {
        this.locked = true;
    }

    /**
     * 打开滚动锁
     **/
    public final void scrollUnlock() {
        this.locked = false;
    }

    /**
     * 是否正在动画
     *
     * @return animating
     **/
    public boolean isAnimating() {
        return this.animating;
    }

    /**
     * 打开抽屉
     **/
    public void open() {
        this.updateBounds();
        if (!this.isOpened()) {
            if (this.getOrientation() == 1) {
                this.setContentPositionY(this.ending);
            } else {
                this.setContentPositionX(0.0F);
            }

            this.expanded = true;
        }

    }

    /**
     * 关闭抽屉
     **/
    public void close() {
        this.updateBounds();
        if (this.isOpened()) {
            if (this.getOrientation() == 1) {
                this.setContentPositionY(this.begening);
            } else {
                this.setContentPositionX((float) (-this.viewContent.getWidth()));
            }

            this.expanded = false;
        }

    }

    /**
     * 动画打开抽屉
     **/
    public void animateOpen() {
        this.updateBounds();
        if (!this.isOpened()) {
            if (this.getOrientation() == 1) {
                this.animate(this.ending - this.begening, 100);
            } else {
                this.animate(0.0F, 100);
            }
        }

    }

    /**
     * 动画关闭抽屉
     **/
    public void animateClose() {
        this.updateBounds();
        if (this.isOpened()) {
            if (this.getOrientation() == 1) {
                this.animate(this.begening - this.getCurrentPosition(), 100);
            } else {
                this.animate((float) (-this.viewContent.getWidth()), 100);
            }
        }

    }

    /**
     * 根据情况进行自由操作
     **/
    public void toggle() {
        this.updateBounds();
        if (this.isOpened()) {
            this.animateClose();
        } else {
            this.animateOpen();
        }

    }

    /**
     * 返回当前位置
     *
     * @return float
     **/
    public float getCurrentPosition() {
        return this.getOrientation() == 1 ? this.getContentPositionY() : this.getContentPositionX();
    }

    /**
     * 更新bounds
     **/
    public void updateBounds() {
        if (this.getOrientation() == 1) {
            if (this.begening == 0.0F) {
                this.begening = this.getContentPositionY();
            }

            this.ending = (float) (((Component) this.getComponentParent()).getHeight() - this.getHeight());
        } else {
            if (this.begening == 0.0F) {
                this.begening = (float) this.viewHandle.getWidth();
            }

            this.ending = (float) this.getWidth();
        }

    }
}


